Skip to content

Commit 2188154

Browse files
authored
Merge pull request #6462 from akatsoulas/survey-tabs-ui
Remove containers from other tabs if user has voted
2 parents a283b34 + fb99ac0 commit 2188154

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

kitsune/sumo/static/sumo/js/survey_form.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1+
const channel = new BroadcastChannel('closeSurveyWidgets');
2+
3+
document.addEventListener('closeSurveyWidgets', (event) => {
4+
const post_message = {
5+
url: event.detail.url,
6+
message: 'closeSurveyWidgets'
7+
}
8+
channel.postMessage(post_message);
9+
10+
});
11+
12+
channel.onmessage = (event) => {
13+
// remove all survey containers from non active tabs if the url matches with active tab
14+
if (event.data.message === 'closeSurveyWidgets') {
15+
const elements = document.querySelectorAll('.document-vote');
16+
const matchAny = Array.from(elements).some(element => {
17+
// Extract the KB slug from both URLs
18+
const elementKbSlug = element.dataset.pageUrl.match(/kb\/([^/]+)/)?.[1];
19+
const eventKbSlug = event.data.url.match(/kb\/([^/]+)/)?.[1];
20+
21+
return elementKbSlug?.toLowerCase() === eventKbSlug?.toLowerCase();
22+
});
23+
if (matchAny) {
24+
elements.forEach(element => element.remove());
25+
}
26+
}
27+
}
28+
29+
130
document.addEventListener('alpine:init', () => {
231
Alpine.data('surveyForm', () => ({
332
selectedReason: '',

kitsune/wiki/jinja2/wiki/includes/document_macros.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ <h3 class="{% if close_button %}sumo-card-heading{% else %}sidebar-subheading fo
352352
{% set header = _('Was this article helpful?') %}
353353
{% endif %}
354354
{% if document.allows_vote(request) and not document.current_revision.has_voted(request) %}
355-
<div class="document-vote elevation-02 text-center">
355+
<div class="document-vote elevation-02 text-center" data-page-url="{{ request.path }}">
356356
<form class="document-vote--form helpful"
357357
hx-post="{{ url('wiki.document_vote', document_slug=document.slug) }}"
358358
hx-indicator=".wait"

kitsune/wiki/views.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1360,7 +1360,9 @@ def handle_vote(request, document_slug):
13601360

13611361
if request.headers.get("HX-Request") and survey_context:
13621362
survey_html = render_to_string("wiki/includes/survey_form.html", survey_context, request)
1363-
return HttpResponse(survey_html)
1363+
response = HttpResponse(survey_html)
1364+
response["HX-Trigger"] = json.dumps({"closeSurveyWidgets": {"url": request.path}})
1365+
return response
13641366
return HttpResponseRedirect(revision.document.get_absolute_url())
13651367

13661368

0 commit comments

Comments
 (0)